package com.gzz.common.base;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.SingleColumnRowMapper;

import lombok.extern.slf4j.Slf4j;

/**
 * @功能描述 【数据访问】基类
 * @author 高振中
 * @date 2021-02-01 20:20:20
 */
@Slf4j
public class BaseJDBC extends BaseTemplate {

	final protected <T extends BaseCondition> Integer count(String sql, T cond) {
		return count(false, sql, cond);
	}

	final protected <T extends BaseCondition> Integer count(boolean show, String sql, T cond) {
		if (show)
			log.info(sql, cond.array());
		return jdbcTemplate.queryForObject(sql, cond.array(), Integer.class);
	}

	final protected Integer update(String sql, Object... obj) {
		return update(false, sql, obj);
	}

	final protected Integer update(boolean show, String sql, Object... obj) {
		if (show)
			log.info(sql, obj);
		return jdbcTemplate.update(sql, obj);
	}

	final protected <T, C extends BaseCondition> List<T> list(String sql, C cond, Class<T> clazz) {
		return list(false, sql, cond, clazz);
	}

	final protected <T, C extends BaseCondition> List<T> list(boolean show, String sql, C cond, Class<T> clazz) {
		if (show)
			log.info(sql, cond.array());
		return jdbcTemplate.query(sql, cond.array(), new BeanPropertyRowMapper<>(clazz));
	}

	final protected <T> List<T> list(String sql, Class<T> clazz, Object... obj) {
		return list(false, sql, clazz, obj);
	}

	final protected <T> List<T> list(boolean show, String sql, Class<T> clazz, Object... obj) {
		if (show)
			log.info(sql, obj);
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(clazz), obj);
	}

	final protected <T> List<T> listSingle(String sql, Class<T> clazz, Object... obj) {
		return listSingle(false, sql, clazz, obj);
	}

	final protected <T> List<T> listSingle(boolean show, String sql, Class<T> clazz, Object... obj) {
		if (show)
			log.info(sql, obj);
		return jdbcTemplate.query(sql, new SingleColumnRowMapper<>(clazz), obj);
	}

	final protected <T> T findById(String sql, Class<T> clazz, Object id) {
		return findById(false, sql, clazz, id);
	}

	final protected <T> T findById(boolean show, String sql, Class<T> clazz, Object id) {
		if (show)
			log.info(sql, id);
		return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(clazz), id);
	}

}